package com.dbgs.insurance.insuredperson.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.dbgs.insurance.insuredperson.R;
import com.dbgs.insurance.insuredperson.activity.register.InputVerifyCodeActivity;
import com.dbgs.insurance.insuredperson.activity.register.RegisterActivity;
import com.dbgs.insurance.insuredperson.cache.GlobalCache;
import com.dbgs.insurance.insuredperson.constants.ConstInt;
import com.dbgs.insurance.insuredperson.http.ApiCallback;
import com.dbgs.insurance.insuredperson.utils.TimeCountUtil;
import com.hss.base.BaseActivty;
import com.wondersgroup.insurance.datalibrary.ApiManager;
import com.wondersgroup.insurance.datalibrary.request.ReqCreateParams;
import com.wondersgroup.insurance.datalibrary.result.ResultBaseBean;
import com.xw.repo.XEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/4/18.
 */

public class LoginActivity extends BaseActivty {


    @BindView(R.id.close)
    AppCompatTextView close;
    @BindView(R.id.xet_name)
    XEditText xetName;
    @BindView(R.id.btn_login)
    AppCompatButton btnLogin;
    @BindView(R.id.register)
    AppCompatTextView register;
    @BindView(R.id.title)
    AppCompatTextView title;

    private TimeCountUtil timeCountUtil;
    public static final int REQUEST_LOGIN_CODE = 11;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    @Override
    public void initView() {
        super.initView();

        title.setText(getResources().getText(R.string.str_login));
        /*if (BuildConfig.DEBUG) {//13761123436  18818232593  156
            xetName.setText("15611111111");
            xedYzm.setText("123456");
        }*/
    }

    @Override
    public void initListener() {
        super.initListener();
    }

    @OnClick({R.id.close, R.id.btn_login, R.id.register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
          /*  case R.id.xed_yzm:
                if (!TextUtils.isEmpty(xetName.getText().toString().trim())) {
                    showToast("请输入手机号！");
                    return;
                }
                //  sendCode();
                break;*/
            case R.id.close:
                finishActivity();
                break;
            case R.id.register:
                startActy(RegisterActivity.class);
                break;
            case R.id.btn_login:
                /*if (!TextUtils.isEmpty(xetName.getText().toString().trim())) {
                    showToast("请输入手机号！");
                    return;
                }
                if (!TextUtils.isEmpty(xedYzm.getText().toString().trim())) {
                    showToast("请输入验证码！");
                    return;
                }*/
                Intent intent = new Intent(LoginActivity.this,
                        InputVerifyCodeActivity.class);
                intent.putExtra(InputVerifyCodeActivity.FLAG, 1);
                startActivityForResult(intent,REQUEST_LOGIN_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_LOGIN_CODE && resultCode==ConstInt.RESULT_VALUE_CHANGED){
            exit();
        }
    }

    public void sendCode(String mobile, int sendType, final AppCompatButton button) {
        showLoadingDialog();
        //addSubscription();
        ApiManager.getInstance().getmApi().sendCode(ReqCreateParams.sendCodeParams(mobile, sendType, GlobalCache.getInstance().getReqBaseParams())).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new ApiCallback<ResultBaseBean>() {
                    @Override
                    public void onSuccess(ResultBaseBean result) {
                        if (result.code == 0) {
                            showToast("发送成功！");
                            // 倒计时
                            timeCountUtil = new TimeCountUtil(
                                    LoginActivity.this, 60000,
                                    1000, button);
                            timeCountUtil.start();
                        } else {
                            showToast(result.message);
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        showToast(msg);
                    }

                    @Override
                    public void onFinish() {
                        hideLoadingDialog();
                    }

                    @Override
                    public void onDataErr(String msg) {
                        showToast(msg);
                    }

                    @Override
                    public void onExitLogin() {

                    }

                    @Override
                    public void onStart() {
                        super.onStart();

                    }
                });
    }
}
